﻿/// <summary>
///Author: Jignesh Chaudhari
///Written: 2013-Jan 20
///Purpose:For registrration of user
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TravelExpertsASP
{
    public partial class registration : System.Web.UI.Page
    {
        //page load event
        protected void Page_Load(object sender, EventArgs e)
        {
            lblCheckEmail.Enabled = false;
            btnReset.Attributes.Add("onclick", "return false;");
           
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            lblCheckEmail.Enabled = false;
            lblCheckEmail.Text = "";
            //validating page
            Page.Validate("grp1");
            Page.Validate("grp2");
            if (!Page.IsValid)
            {

                return;
            }

            //creating object of DbAcess class
            DbAccess objDb = new DbAccess();
            string strEmail = txtEmail.Text;
           
            //searching for email id in db, if it exist in database or not and if it exsist in DB.. give error
            foreach (Customer cust in objDb.SelectCustomer())
            {
                if (strEmail == cust.CustEmail)
                {
                    lblCheckEmail.Enabled = true;
                    lblCheckEmail.Text = "This email address is already used.";

                    return;
                }   
                
            }

            objDb.InsertCustomer(getCustomer());//Calling function to get object of customer class 


            Response.Redirect("Login.aspx");

        }

        //geting object of customer class to insert new customer in database

        public Customer getCustomer()
        {
            Customer objCust = new Customer();
            objCust.CustAddress = txtAddress.Text;
            objCust.CustBusPhone = txtBusPhone.Text;
            objCust.CustCity = txtCity.Text;
            objCust.CustCountry = txtCountry.Text;
            objCust.CustEmail = txtEmail.Text;
            objCust.CustFirstName = txtFirstName.Text;
            objCust.CustHomePhone = txtHomePhone.Text;
            objCust.CustLastName = txtLastName.Text;
            if (!(txtAgtId.Text.Trim() == ""))
            {
                objCust.AgentId = Convert.ToInt32(txtAgtId.Text);
            }
            else
            {
                objCust.AgentId = 1;
            }
            
            objCust.CustPostal = txtPostal.Text;
            objCust.CustProv = txtProv.Text;
            objCust.CustPWord = txtPassword.Text;

            return objCust;
        
        }


    }
}